#!/usr/bin/python
# -*- coding: utf-8 -*-
#-------------------------------------------------
#Create At: 2009-4-21 下午03:14:32 
#Create By: Fla.sam
#
#DESC: 
#
#-------------------------------------------------

from annoying.decorators import autostrip
from django import forms
from django.forms import ModelForm
from core_app.models import Department, PureUser
from uni_form.helpers import FormHelper, Submit, Reset
from annoying.functions import get_object_or_None


class StudentRegForm(ModelForm):
    
    password1 = forms.CharField(label='密码',widget=forms.PasswordInput(render_value=False))
    password2 = forms.CharField(label='再次输入密码',widget=forms.PasswordInput(render_value=False))
    
    helper = FormHelper()
    helper.form_id = 'reg_ftp'
    helper.form_class = 'reg'

    # add in a submit and reset button
    submit = Submit(u'submit',u'提交')
    helper.add_input(submit)
    reset = Reset('reset',u'重置表单')
    helper.add_input(reset)
    
    def clean_password2(self):
        password1 = self.cleaned_data.get("password1", "")
        password2 = self.cleaned_data["password2"]
        if password1 != password2:
            raise forms.ValidationError("两次输入的密码不一致,请重新输入")
        if len(password2) < 6:
            raise forms.ValidationError("密码强度不足,请重新输入")
        return password2
    
    class Meta:
        model = PureUser
        fields = ('loginname', 'name', 'gender', 'department')
StudentRegForm =autostrip(StudentRegForm)

class StudentStatusForm(forms.Form):

    name = forms.CharField(max_length=23,label=u'用户名', help_text=u'学生请输入自己的学号作为登陆用户名,教师请直接输入用户名')
    
    helper = FormHelper()
    helper.form_id = 'user_status'
    helper.form_class = 'reg'
    
    # add in a submit and reset button
    submit = Submit(u'submit',u'提交')
    helper.add_input(submit)
    reset = Reset('reset',u'重置表单')
    helper.add_input(reset)
    
    def clean(self):
        if 'name' in self.cleaned_data:
            _name = self.cleaned_data['name']
            _user_obj = get_object_or_None(PureUser, loginname=_name)
            if _user_obj == None:
                raise forms.ValidationError(u'用户未找到, 请重新输入')
        else:
            raise forms.ValidationError(u'请输入用户名')
        self.cleaned_data['user_obj'] = _user_obj
        return self.cleaned_data
    
    def save(self):
        return self.cleaned_data['user_obj']
    
StudentStatusForm = autostrip(StudentStatusForm)
    